let index = new Vue({
    el: '#app',
    data() {
        return {
            goods: {},
            goodsName: '',
            login: {
                code: '',
                pass: '',
                success: false,
                errorMsg: '',
                name: ''
            },
            carts: [],
            order: {
                user: {
                    id: 1,
                    name: '',
                    sex: '',
                    phone: '',
                    address: '',
                    address_list: [],  // 常用收货人地址列表
                    flagAddress: false  // 是否有收货人地址
                },
                // 收货人信息
                shouhuoren: {
                    contact: '',
                    addressDesc: '',
                    tel: '',
                    postCode: '',
                    id: 1
                }
            }
        }
    },
    created() {
        this.loadIndexData(1)
        this.isLogin()
        this.loadOrderUser()
        if (sessionStorage.getItem("cart") === "1") {
            this.showCart()
        }
    },
    methods: {
        loadIndexData(page) {
            let _this = this;
            axios.get('http://localhost:8051/goods/' + page)
                .then((response) => {
                    _this.goods = response.data.data;
                })
        },
        searchGoods() {
            let _this = this;
            let goodsName = this.goodsName + " ";
            axios.get('http://localhost:8051/goods/' + goodsName + "/0")
                .then((response) => {
                    _this.goods = response.data.data;
                })
        },
        userLogin() {
            let _this = this;
            axios.post('http://localhost:8011/user/' + _this.login.code + '/' + _this.login.pass)
                .then((response) => {
                    let result = response.data;
                    if (result.success) {
                        localStorage.setItem('token', result.data);
                        _this.login.success = true;
                        _this.login.name = result.errorMsg;
                    } else {
                        _this.login.errorMsg = result.errorMsg;
                        _this.login.success = false;
                    }
                })
        },
        isLogin() {
            let _this = this;
            let token = localStorage.getItem('token');
            if (token === null || token === undefined) {
                this.login.success = false;
                return false;
            }
            axios.get('http://localhost:8011/user/is', {
                headers: {
                    token: token
                }
            }).then((response) => {
                // console.log('isLogin..')
                let result = response.data;
                if (result.success) {
                    _this.login.success = true;
                    _this.login.name = result.data;
                    return true;
                } else {
                    _this.login.success = false;
                    return false;
                }
            })
        },
        logout() {
            let _this = this;
            let token = localStorage.getItem('token');
            if (token === null || token === undefined) {
                return;
            }
            axios.get('http://localhost:8011/user/logout', {
                headers: {
                    token: token
                }
            }).then((response) => {
                let result = response.data;
                if (result.success) {
                    _this.login.success = false;
                    _this.login.code = '';
                    _this.login.pass = '';
                    localStorage.removeItem('token');
                } else {
                    _this.login.success = true;
                }
            })
        },
        addCart(gid) {
            let _this = this;
            this.isLogin();
            let token = localStorage.getItem('token');
            if (this.login.success) {
                axios.get('http://localhost:8033/cart/' + gid + '/1', {
                    headers: {
                        token: token
                    }
                }).then((response) => {
                    _this.login.errorMsg = response.data.data;
                });
            } else {
                this.login.errorMsg = '请登录后在加入购物车';
            }
        },
        toCart() {
            sessionStorage.setItem("cart", "1");
            location.href = 'cart.html';
        },
        showCart() {
            //console.log('进入了查看购物车方法！')
            //sessionStorage.removeItem("cart");
            let _this = this;
            this.isLogin();
            let token = localStorage.getItem('token');
            //this.login.success = true
            //console.log('开始查看购物车信息')
            axios.get('http://localhost:8033/cart', {
                headers: {
                    token: token
                }
            }).then((response) => {
                let result = response.data;
                _this.carts = result.data;
            })
        },
        removeCart(gid) {
            let _this = this;
            this.isLogin();
            let token = localStorage.getItem('token');
            this.login.success = true
            if (this.login.success) {
                //console.log('开始查看购物车信息')
                axios.delete('http://localhost:8033/cart/' + gid, {
                    headers: {
                        token: token
                    }
                }).then((response) => {
                    let result = response.data;
                    _this.carts = result.data;
                })
            }
        },
        updateCart(gid, num) {
            if (num < 1) {
                return;
            }
            console.log('更新了,' + gid + '，新数量:' + num)
            let _this = this;
            this.isLogin();
            let token = localStorage.getItem('token');
            this.login.success = true
            if (this.login.success) {
                //console.log('开始查看购物车信息')
                axios.get('http://localhost:8033/cart/update/' + gid + '/' + num, {
                    headers: {
                        token: token
                    }
                }).then((response) => {
                    let result = response.data;
                    _this.carts = result.data;
                })
            }
        },
        loadOrderUser() {
            let _this = this;
            axios.get('http://localhost:8011/user/id/token', {
                headers: {
                    token: localStorage.getItem('token')
                }
            }).then((response) => {
                let result = response.data;
                _this.order.user.id = result.data.id;
                _this.order.user.name = result.data.userName;
                _this.order.user.sex = result.data.userSex;
                _this.order.user.phone = result.data.phone;
                _this.order.user.address = result.data.address;
                _this.loadUserAddress(result.data.id)
            });
        },
        loadUserAddress(uid) {
            let _this = this;
            axios.get('http://localhost:8019/address/' + uid).then((response) => {
                if (response.data.success) {
                    _this.order.user.address_list = response.data.data;
                    let l = _this.order.user.address_list;
                    for (let i = 0; i < l.length; i++) {
                        let o = l[i];
                        if (o.defaultAddress === 1) {
                            _this.order.shouhuoren.id = o.id;
                        }
                    }
                    _this.order.user.flagAddress = true;
                } else {
                    _this.order.user.flagAddress = false;
                }

            })
        },
        saveAddress() {
            let _this = this;
            let contact = this.order.shouhuoren.contact;
            let addressDesc = this.order.shouhuoren.addressDesc;
            let postCode = this.order.shouhuoren.postCode;
            let tel = this.order.shouhuoren.tel;
            let id = _this.order.user.id;
            axios.get('http://localhost:8019/address/' + contact + '/' + addressDesc + '/' + postCode + '/' + tel + '/' + id)
                .then((response) => {
                    if (response.data.success) {
                        _this.order.user.address_list = response.data.data;
                        _this.order.user.flagAddress = true;
                    } else {
                        _this.order.user.flagAddress = false;
                    }
                });
        },
        pay() {
            location.href = 'http://localhost:8031/alipay/pay/' + this.order.shouhuoren.id + '/' + sessionStorage.getItem('total') + '/' + localStorage.getItem('token');
        }
    },
    computed: {
        totalPrice() {
            let total = this.carts.reduce((total, item) => {
                return total + item.goods.data.goodsPrice * item.num;
            }, 0);
            sessionStorage.setItem('total', total);
            return total;
        }
    }
});